package com.liao.myblog.controller.blog;

import com.liao.myblog.enums.ResultCode;
import com.liao.myblog.pojo.Blog;
import com.liao.myblog.pojo.Catalogue;
import com.liao.myblog.pojo.Comment;
import com.liao.myblog.service.BlogService;
import com.liao.myblog.service.CatalogueService;
import com.liao.myblog.service.CommentService;
import com.liao.myblog.vo.BlogPage;
import com.liao.myblog.vo.ResultVO;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class MyBlogController {
    @Resource
    private BlogService blogService;
    @Resource
    private CommentService commentService;
    @Resource
    private CatalogueService catalogueService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @GetMapping(value = {"/", "/index", "/index.html"})
    public String index() {
        return "blog/amaze/index";
    }

    @ResponseBody
    @PostMapping(value = "/search")
    public ResultVO<Object> getBlogByPage(@RequestBody Map<String, Object> map) {
        int limit = (int) map.get("limit");
        int offset = (int) map.get("offset");
        String keyword = (String) map.get("keyword");
        if (keyword == null) {
            keyword = "";
        }
        List<Blog> blogs = blogService.findBlogByPaging(limit, offset * limit, keyword);
        if (blogs != null) {
            return new ResultVO<>(blogs);
//            return ResultUtil.getResult(blogs, ResultUtil.RESULT_SUCCESS);
        } else {
            return new ResultVO<>(ResultCode.ERROR);
//            return ResultUtil.getResult("失败", ResultUtil.RESULT_ERROR);
        }
    }

    @ResponseBody
    @PostMapping(value = "/blogCount")
    public ResultVO<Object> getBlogCount() {
        int blogCount = blogService.findBlogCount();
        return new ResultVO<>(blogCount);
//        return ResultUtil.getResult(blogCount, ResultUtil.RESULT_SUCCESS);
    }

    @ResponseBody
    @PostMapping(value = "/newBlog")
    public ResultVO<Object> getNewBlog() {
        List<Blog> blogs = blogService.queryNewBlog(5);
        if (blogs != null) {
//            return ResultUtil.getResult(blogs, ResultUtil.RESULT_SUCCESS);
            return new ResultVO<>(blogs);
        } else {
            return new ResultVO<>(ResultCode.ERROR);
//            return ResultUtil.getResult(blogs, ResultUtil.RESULT_ERROR);
        }
    }

    @ResponseBody
    @PostMapping(value = "/hotBlog")
    public ResultVO<Object> getHotBlog() {
        List<Blog> blogs = blogService.queryHotBlog(5);
        if (blogs != null) {
            return new ResultVO<>(blogs);
//            return ResultUtil.getResult(blogs, ResultUtil.RESULT_SUCCESS);
        } else {
            return new ResultVO<>(ResultCode.ERROR);
        }
    }

    @GetMapping(value = "/detail")
    public ModelAndView toDetail(int id) {
        ModelAndView view = new ModelAndView();
        if (id <= 0) {
            view.setViewName("error/error_404");
        } else {
            view.addObject("id", id);
            view.setViewName("blog/amaze/detail");
        }
        return view;
    }

    @ResponseBody
    @RequestMapping(value = "/gbcbc", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultVO<Object> getBlogCountByCatalogue(@RequestParam(value = "catalogueId") int catalogueId) {
        if (catalogueId < 0) {
            return new ResultVO<>(ResultCode.VALIDATE_FAILED);
        }
        return new ResultVO<>(blogService.queryBlogCountByCatalogueId(catalogueId));
    }

    @ResponseBody
    @RequestMapping(value = "/gbbc", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultVO<Object> getBlogByCatalogue(@RequestParam(value = "catalogueId") int catalogueId,
                                               @RequestParam(value = "pageNum") int pageNum,
                                               @RequestParam(value = "pageSize") int pageSize) {
        if (catalogueId < 0) {
            return new ResultVO<>(ResultCode.VALIDATE_FAILED);
        }
        System.out.println("pageNum="+pageNum+"pageSize="+pageSize);
        return new ResultVO<>(blogService.queryBlogByCatalogueId(catalogueId,pageNum,pageSize));
    }

    @ResponseBody
    @GetMapping(value = "/blog/{id}")
    public ResultVO<Object> getBlog(@PathVariable(value = "id") int id) {
        if (id <= 0) {
            return new ResultVO<>(ResultCode.VALIDATE_FAILED);
//            return ResultUtil.getResult("参数错误", ResultUtil.RESULT_ERROR);
        }
        Blog blog = blogService.queryBlogById(id);
        System.out.println(blog);
        if (blog != null) {
            blog.setViews(blog.getViews() + 1);
            System.out.println("MyBlogController.getBlog" + blog);
            blogService.updateBlogById(blog);
            return new ResultVO<>(blog);
//                return ResultUtil.getResult(blog, ResultUtil.RESULT_SUCCESS);
        } else {
            return new ResultVO<>(ResultCode.ERROR);
        }
    }

    @ResponseBody
    @PostMapping(value = "/comment/{id}")
    public ResultVO<Object> getCommentByBlogId(@PathVariable(value = "id") int id,
                                               @RequestBody Map<String, Object> map) {
        if (id <= 0) {
            return new ResultVO<>(ResultCode.VALIDATE_FAILED);
//            return ResultUtil.getResult("错误", ResultUtil.RESULT_ERROR);
        }
        int limit = (int) map.get("limit");
        int offset = (int) map.get("offset");
        List<Comment> comments = commentService.queryCommentByPage(limit, offset, id);
        if (comments != null) {
            return new ResultVO<>(comments);
//            return ResultUtil.getResult(comments, ResultUtil.RESULT_SUCCESS);
        } else {
            return new ResultVO<>(ResultCode.ERROR);
//            return ResultUtil.getResult(null, ResultUtil.RESULT_ERROR);
        }
    }

    @ResponseBody
    @GetMapping(value = "/commentCount")
    public ResultVO<Object> queryCommentCount() {
        int i = commentService.queryCommentCount();
//        return ResultUtil.getResult(i, ResultUtil.RESULT_SUCCESS);
        return new ResultVO<>(i);
    }

    @ResponseBody
    @GetMapping(value = "/count/{id}")
    public ResultVO<Object> queryCommentCountByBlogId(@PathVariable(value = "id") int id) {
        int i = commentService.queryCommentCountByBlogId(id);
//        return ResultUtil.getResult(i, ResultUtil.RESULT_SUCCESS);
        return new ResultVO<>(i);
    }

    @ResponseBody
    @PostMapping(value = "/addComment")
    public ResultVO<Object> addComment(@RequestBody Map<String, Object> map) {
        String content = (String) map.get("commentBody");
        String blogIds = (String) map.get("blogId");
        String verifyCode = (String) map.get("verifyCode");
        int blogId = Integer.parseInt(blogIds);
        String email = (String) map.get("email");
        String commentator = (String) map.get("commentator");

        String s = redisTemplate.opsForValue().get("commentVerifyCode");
        System.out.println("verifyCode=" + s);
        if (s == null || s.length() == 0) {
            return new ResultVO<>(ResultCode.EXPIRED_VERIFYCODE);
//            return ResultUtil.getResult("验证码过期", ResultUtil.RESULT_ERROR);
        }

        if (!s.equals(verifyCode)) {
            return new ResultVO<>(ResultCode.ERROR_VERIFYCODE);
//            return ResultUtil.getResult("验证码错误", ResultUtil.RESULT_ERROR);
        }

        Comment comment = new Comment();
        comment.setContent(content);
        comment.setIsDelete((byte) 0);
        comment.setCommentStatus((byte) 0);
        comment.setTime(new Date());
        comment.setBlogId(blogId);
        comment.setEmail(email);
        comment.setUsername(commentator);
        System.out.println(comment);
        int i = commentService.addComment(comment);
        if (i > 0) {
            return new ResultVO<>("评论成功");
//            return ResultUtil.getResult("评论成功", ResultUtil.RESULT_SUCCESS);
        } else {
            return new ResultVO<>(ResultCode.ERROR);
//            return ResultUtil.getResult("评论失败", ResultUtil.RESULT_ERROR);
        }
    }

    @ResponseBody
    @PostMapping(value = "/getBlogByKeyword")
    public BlogPage a(@RequestBody Map<String, Object> map) {
        String keyword = (String) map.get("keyword");
        int limit = (int) map.get("limit");
        int offset = (int) map.get("offset");
        return blogService.queryBlogByKeyword(limit, offset, keyword);
    }

    @ResponseBody
    @PostMapping(value = "/getCountByKeyword")
    public int getCountByKeyword(@RequestBody Map<String, Object> map) {
        String keyword = (String) map.get("keyword");
        return blogService.queryBlogCountByKeyword(keyword);
    }

    @ResponseBody
    @GetMapping(value = "/catalogue/list")
    private List<Catalogue> catalogue() {
        return catalogueService.findCatalogueList();
    }

}
